<template>
  <el-card>
    <el-table :data="skuArr" border style="margin: 10px 0px">
      <el-table-column label="序号" type="index" align="center" width="100px"></el-table-column>
      <el-table-column label="名称" prop="skuName" align="center" width="200px"></el-table-column>
      <el-table-column label="描述" prop="skuDesc" align="center" width="330px"></el-table-column>
      <el-table-column label="图片" width="200px" align="center">
        <template #="{ row, $index }">
          <img :src="row.skuDefaultImg" style="width: 150px; height: 150px" />
        </template>
      </el-table-column>
      <el-table-column label="重量" prop="weight" width="150px"></el-table-column>
      <el-table-column label="价格" prop="price" width="150px"></el-table-column>
      <el-table-column label="操作" width="300px" fixed="right">
        <template #="{ row, $index }">
          <el-button
            :type="row.isSale == 1 ? 'success' : 'info'"
            :color="row.isSale == 1 ? '#0ba54b' : '#3f4134'"
            size="small"
            @click="updateSale(row)"
            :icon="row.isSale == 1 ? 'Bottom' : 'Top'"
            :title="row.isSale == 1 ? '下架' : '上架'"
            v-has="`btn.Sku.updown`"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            @click="updateSku(row)"
            icon="Edit"
            title="编辑"
            v-has="`btn.Sku.update`"
          ></el-button>
          <el-button
            type="info"
            size="small"
            icon="InfoFilled"
            title="查看"
            @click="findSku(row)"
            v-has="`btn.Sku.detail`"
          ></el-button>
          <el-popconfirm title="确定删除吗?" @confirm="deleteSku(row)" width="200px">
            <template #reference>
              <el-button
                type="danger"
                size="small"
                icon="Delete"
                title="删除"
                v-has="`btn.Sku.remove`"
              ></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[3, 5, 10, 20]"
      :background="true"
      layout="prev, pager, next, jumper, ->, sizes, total"
      :total="total"
      @current-change="getHasSku"
      @size-change="sizeChange"
    />
    <!-- 抽屉组件：展示商品的详情 -->
    <el-drawer v-model="drawer" direction="rtl" size="40%">
      <template #header>
        <h4>查看商品详情</h4>
      </template>
      <template #default>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ skuInfo.price }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag
              style="margin: 3px 5px"
              type="primary"
              v-for="item in skuInfo.skuAttrValueList"
              :key="item.id"
              >{{ item.valueName }}</el-tag
            >
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag
              style="margin: 3px 5px"
              type="primary"
              v-for="item in skuInfo.skuSaleAttrValueList"
              :key="item.id"
              >{{ item.saleAttrValueName }}</el-tag
            >
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <el-carousel :interval="4000" type="card" height="200px">
              <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                <img :src="item.imgUrl" style="width: 100%; height: auto" />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
  </el-card>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import {
  reqSkuList,
  reqSaleSku,
  reqCancelSaleSku,
  reqSkuInfo,
  reqRemoveSku
} from '@/api/product/sku'
import type { SkuResponseData, SkuData, SkuInfoData } from '@/api/product/sku/type'
import { ElMessage } from 'element-plus'

let pageNo = ref<number>(1)
let pageSize = ref<number>(5)
let total = ref<number>(0)

//存储已有的sku
let skuArr = ref<SkuData[]>([])

//控制抽屉显示与隐藏
let drawer = ref<boolean>(false)

let skuInfo = ref<SkuData>({
  category3Id: '',
  spuId: '',
  tmId: '',
  skuName: '',
  price: '',
  weight: '',
  skuDesc: '',
  skuAttrValueList: [],
  skuSaleAttrValueList: [],
  skuDefaultImg: '',
  skuImageList: []
})

onMounted(() => {
  getHasSku()
})

//此方法执行可以获取全部的已有的SKU
const getHasSku = async (pager = 1) => {
  pageNo.value = pager
  let result: SkuResponseData = await reqSkuList(pageNo.value, pageSize.value)

  if (result.code == 200) {
    skuArr.value = result.data.records
    total.value = result.data.total
  }
}

const sizeChange = () => {
  getHasSku()
}

//商品的上架与下架
const updateSale = async (row: SkuData) => {
  if (row.isSale == 1) {
    let result = await reqCancelSaleSku(row.id as number)
    if (result.code == 200) {
      ElMessage({
        type: 'success',
        message: '下架成功！'
      })
      //重新获取
      getHasSku(pageNo.value)
    } else {
      ElMessage({
        type: 'error',
        message: '下架失败！'
      })
    }
  } else {
    let result2 = await reqSaleSku(row.id as number)
    if (result2.code == 200) {
      ElMessage({
        type: 'success',
        message: '上架成功！'
      })
      //重新获取
      getHasSku(pageNo.value)
    } else {
      ElMessage({
        type: 'error',
        message: '上架失败！'
      })
    }
  }
}

//编辑按钮
const updateSku = (row: SkuData) => {
  ElMessage({
    type: 'success',
    message: '程序员正努力开发此功能~~~'
  })
}

//查看sku详情的回调
const findSku = async (row: SkuData) => {
  drawer.value = true
  let result: SkuInfoData = await reqSkuInfo(row.id as number)
  if (result.code == 200) {
    skuInfo.value = result.data
  }
}

//删除按钮的回调
const deleteSku = async (row: SkuData) => {
  let result = await reqRemoveSku(row.id as number)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功！'
    })
    getHasSku(skuArr.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败！'
    })
  }
}
</script>

<style lang="scss" scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
